#include <algorithm>
#include <sstream>
#include <ctime>
#include <iomanip>
#include "FileInfo.h"


using namespace std;

FileInfo::FileInfo(string filename, string owner) : filename(filename), version(0), checkedOut(false), checker("")
{
    owners.push_back(owner);
    revisions.push_back(RevInfo(owner, ""));
    //ctor
}

bool FileInfo::ownedBy(std::string user)
{
    return find(owners.begin(), owners.end(), user) != owners.end();
}

void FileInfo::checkIn(std::string user, std::string comment)
{
    version ++;
    revisions.push_back(RevInfo(user, comment));
    checkedOut = false;
}


string FileInfo::toFileList()
{
    stringstream ss;
    ss << filename << ", ";
    if(checkedOut)
        ss << checker;
    else
        ss << version;
    ss << " (owners: ";
    for(unsigned int i = 0; i < owners.size(); ++i)
    {
        ss << owners[i];
        if(i != owners.size()-1)
            ss << ", ";
    }
    ss << ')';
    return ss.str();
}

std::string FileInfo::getVersionString(int v)
{
    stringstream ss;
    ss << "Ver. " << v << ": " << revisions.at(v).user << ", " << revisions.at(v).date;
    if(v != 0)
        ss <<", " <<  revisions[v].comment;
    return ss.str();
}




std::string RevInfo::getCurrentDate()
{
    time_t t = time(0);   // get time now
    tm * now = localtime( & t );
    stringstream ss;
    ss << setw(2) << setfill('0') << now->tm_mday << '/';
    ss << setw(2) << setfill('0')<< now->tm_mon + 1 << '/' ;
    ss << setw(2) << setfill('0') << (now->tm_year)% 100 << ' ' ;
    ss << setw(2) << setfill('0') << now->tm_hour << ':' ;
    ss << setw(2) << setfill('0') << now->tm_min << ':' ;
    ss << setw(2) << setfill('0') << now->tm_sec;
    return ss.str();
}
